package cn.biplam.back.operator.systemManage.sysprivi.user.table;

import java.util.ArrayList;

import cn.biplam.back.sql.dml.Insert;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.interfaces.DataOperator;
import cn.biplam.common.opcode.SysManageCode.UserRoleOptCode;
import cn.biplam.common.struc.SysStruc.RoleIndexStruc;
import cn.biplam.common.struc.SysStruc.UserRoleStruc;
import cn.biplam.common.wrapper.ErrorMessage;

/**
 *  用户角色表操作类
 * @author XiaoRuxing Dec 14, 200911:24:43 AM
 * @version 1.0
 */
public class UserRoleOpt extends DataOperator{

	/**用户角色表名称*/
	public static final String Table_Name=UserRoleStruc.Table_Name;

	static final String Op_ID=UserRoleOptCode.Op_ID;
	
	public UserRoleOpt() throws ErrorMessage {
		super(Table_Name);
	}
	
	@Override
	protected OperatorResult _exchange() throws ErrorMessage {
		ArrayList<String> sql = new ArrayList<String>();
		if(UserRoleOptCode.Query_By_UserId.equals(opId)){
			this.queryByUserId(sql, filter);
			return this.doing(sql,Operator_Type.Select);
		}else if(UserRoleOptCode.Add.equals(opId)){
			this.addUserRole(sql, filter);
			return this.doing(sql,Operator_Type.DML);
		}else if(UserRoleOptCode.Query_By_RoleId.equals(opId)){
			this.queryByRoleId(sql, filter);
			return this.doing(sql,Operator_Type.Select);
		}else if(UserRoleOptCode.Query_User_Role_Exist.equals(opId)){
			this.queryExistRole(sql, filter);
			return this.doing(sql,Operator_Type.Select);
		}else{
			throw new ErrorMessage("Operator Exchange outport not found:"+ opId+" !");
		}
	}
	
	/**
	 *  查询用户拥有的角色(用于添加用户角色时角色选择树过滤)
	 *  参数：UserRoleStruc.USER_ID,Datafilter默认参数
	 */
	private void queryExistRole(ArrayList<String> sql, DataFilter filter){
		StringBuilder sp = new StringBuilder();
		sp.append(" select b.*,a.* from ");
		sp.append(this.tableName);
		sp.append(" a join ");
		sp.append(RoleIndexStruc.Table_Name);
		sp.append(" b on ");
		sp.append(" a.");
		sp.append(UserRoleStruc.ROLE_ID);
		sp.append("=b.");
		sp.append(RoleIndexStruc.Role_ID);
		sp.append(" where a.");
		sp.append(UserRoleStruc.USER_ID);
		sp.append("='");
		sp.append(filter.getValue(UserRoleStruc.USER_ID));
		sp.append("'");
		sql.add(sp.toString());
	}
	
	/**
	 *  根据用户ID获取用户具有的角色
	 *  参数:UserRoleStruc.USER_ID
	 */
	private void queryByUserId(ArrayList<String> sql, DataFilter filter){
		StringBuilder sp = new StringBuilder();
		sp.append(" select * from ");
		sp.append(this.tableName);
		sp.append(" where ");
		sp.append(UserRoleStruc.USER_ID);
		sp.append("='");
		sp.append(filter.getValue(UserRoleStruc.USER_ID));
		sp.append("'");
		sql.add(sp.toString());
	}
	
	/**
	 * 根据角色ID查询拥有角色的用户列表 
	 * 参数:UserRoleStruc.Role_ID
	 */
	private void queryByRoleId(ArrayList<String> sql, DataFilter filter){
		StringBuilder sp = new StringBuilder();
		sp.append(" select * from ");
		sp.append(this.tableName);
		sp.append(" where ");
		sp.append(UserRoleStruc.ROLE_ID);
		sp.append("='");
		sp.append(filter.getValue(UserRoleStruc.ROLE_ID));
		sp.append("'");
		sql.add(sp.toString());
	}
	
	/**
	 *  添加用户角色
	 */
	private void addUserRole(ArrayList<String> sql, DataFilter filter) throws ErrorMessage{
		Insert insert = new Insert(this.tableName);
		String userRoleId = this.generatorRowID();
		insert.putStringFieldDenyNull(UserRoleStruc.USER_ROLE_ID, userRoleId);
		insert.putStringFieldDenyNull(UserRoleStruc.USER_ID, filter.getValue(UserRoleStruc.USER_ID));
		insert.putStringFieldDenyNull(UserRoleStruc.ROLE_ID, filter.getValue(UserRoleStruc.ROLE_ID));
		sql.add(insert.getSimpleSql());
	}
}
